const uploadFile = (file, config) => {
    const { url } = config;
    const { sessionToken, tmpSecretId, tmpSecretKey } = config.credentials;
    wx.showLoading({ title: '上传中...', mask: true });
    return new Promise((resolve, reject) => {
        var fileUrl = [];
        file.forEach(item => {
            const extName = item.url.split('.').pop();
            const key = `app/xcxy/${new Date().getTime()}.${extName}`;
            wx.uploadFile({
                filePath: item.url,
                name: 'file',
                url: url,
                formData: {
                    key: key,
                    sessionToken,
                    tmpSecretId,
                    tmpSecretKey
                },
                success: res => {
                    const { statusCode, header } = res;
                    if (statusCode !== 204) {
                        wx.showToast({ title: "上传失败", icon: 'none' });
                        reject(res.errMsg);
                        return;
                    }
                    fileUrl.push(header.Location);
                    if (fileUrl.length >= file.length) {
                        resolve(fileUrl);
                    }
                },
                fail: err => {
                    wx.showToast({ title: "上传失败", icon: 'none' });
                    reject(err);
                },
                complete: () => {
                    wx.hideLoading();
                }
            });
        });
    });
}

export default uploadFile;